package com.mall.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.pojo.User;
import com.mall.service.UserService;
import com.mall.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 简单案例 - 用户表 前端控制器
 * </p>
 *
 * @author 戴浩军
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    @RequestMapping("/login")
    public Msg login(@RequestBody User user, HttpSession session) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username", user.getUserName());
        qw.eq("password", SecureUtil.md5(user.getPassword()));
        User one = userService.getOne(qw);
        boolean isOk = one != null && one.getId() != null;
//        System.out.println(one);
        if (isOk) {
            session.setAttribute("user",one);
            return Msg.success("登陆成功！");
        } else {
            return Msg.fail("登陆失败");
        }


    }

    @PostMapping("/regeist")
    public Msg regeist(@RequestBody User user) {
        System.out.println(user);
        String str = SecureUtil.md5(user.getPassword());
        user.setPassword(str);
        if (user.getUserName() != "" & str != "") {
            userService.save(user);
            return Msg.success();
        }
        return Msg.fail();

    }


}

